package com.ruoyi.system.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.InvoiceGroup;
import com.ruoyi.system.service.impl.InvoiceGroupServiceImpl;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.InvoiceRecordsAudit;
import com.ruoyi.system.service.IInvoiceRecordsAuditService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 中心库月结审计Controller
 * 
 * @author ruoyi
 * @date 2024-06-28
 */
@RestController
@RequestMapping("/system/audit")
public class InvoiceRecordsAuditController extends BaseController
{
    @Autowired
    private IInvoiceRecordsAuditService invoiceRecordsAuditService;
    @Autowired
    private InvoiceGroupServiceImpl invoiceGroupService;

    /**
     * 查询中心库月结审计列表
     */
    @GetMapping("/list")
    public TableDataInfo list(InvoiceRecordsAudit invoiceRecordsAudit)
    {
        startPage();
        List<InvoiceRecordsAudit> list = invoiceRecordsAuditService.selectInvoiceRecordsAuditList(invoiceRecordsAudit);
        return getDataTable(list);
    }

    /**
     * 导出中心库月结审计列表
     */
    @Log(title = "中心库月结审计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InvoiceRecordsAudit invoiceRecordsAudit)
    {
        List<InvoiceRecordsAudit> list = invoiceRecordsAuditService.selectInvoiceRecordsAuditList(invoiceRecordsAudit);
        ExcelUtil<InvoiceRecordsAudit> util = new ExcelUtil<InvoiceRecordsAudit>(InvoiceRecordsAudit.class);
        util.exportExcel(response, list, "中心库月结审计数据");
    }

    /**
     * 获取中心库月结审计详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(invoiceRecordsAuditService.selectInvoiceRecordsAuditById(id));
    }

    /**
     * 新增中心库月结审计
     */
    @Log(title = "中心库月结审计", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InvoiceRecordsAudit invoiceRecordsAudit)
    {
        return toAjax(invoiceRecordsAuditService.insertInvoiceRecordsAudit(invoiceRecordsAudit));
    }

    /**
     * 修改中心库月结审计
     */
    @Log(title = "中心库月结审计", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InvoiceRecordsAudit invoiceRecordsAudit)
    {
        return toAjax(invoiceRecordsAuditService.updateInvoiceRecordsAudit(invoiceRecordsAudit));
    }

    /**
     * 删除中心库月结审计
     */
    @Log(title = "中心库月结审计", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(invoiceRecordsAuditService.deleteInvoiceRecordsAuditByIds(ids));
    }

    /**
     * 页面列表数据
     */
    @GetMapping("/listWebInfo")
    public TableDataInfo listWebInfo(@RequestParam(value = "productName",required = false)String productName,
                                     @RequestParam(value = "provide",required = false)String provide,
                                     @RequestParam(value = "company",required = false)String company,
                                     @RequestParam(value = "status",required = false)String status,
                                     @RequestParam(value = "productGroup",required = false)String productGroup,
                                     @RequestParam(value = "startTime",required = false)String startTime)
    {
        startPage();
        List<Map<String,Object>> list = invoiceRecordsAuditService.listWebInfo(productName,provide,company,status,productGroup,startTime);
        Double allMoney = invoiceRecordsAuditService.selectAllMoney(provide, startTime);
        Double sumMoney = invoiceRecordsAuditService.selectSumProductMoney(provide, startTime);
        for (Map<String,Object> map:list) {
            String productCode = map.get("product_code").toString();
            InvoiceGroup invoiceGroup = new InvoiceGroup();
            invoiceGroup.setProductCode(productCode);
            List<InvoiceGroup> invoiceGroups = invoiceGroupService.selectInvoiceGroupList(invoiceGroup);
            if(invoiceGroups!=null){
                map.put("groupList",invoiceGroups);
            }
            map.put("allMoney",allMoney);
            map.put("sumMoney",sumMoney);
        }
        return getDataTable(list);
    }
}
